package top.kuxiao.crmserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.kuxiao.crmserver.pojo.Contact;
import top.kuxiao.crmserver.pojo.Customer;
import top.kuxiao.crmserver.service.IContactService;
import top.kuxiao.crmserver.service.ICustomerService;
import top.kuxiao.crmserver.utils.Auth;
import top.kuxiao.crmserver.utils.ResultUtil;

import java.util.List;

/**
 * <p>
 * 联系人 前端控制器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-17
 */
@Api(tags ="客户联系人")
@CrossOrigin
@RestController
@RequestMapping("/contact")
public class ContactController {

    @Autowired
    IContactService contactService;

    @Autowired
    ICustomerService customerService;


    /**
     * 显示该客户的全部联系人
     */
    @ApiImplicitParam(name = "id", value = "客户的id")
    @ApiOperation(value = "该客户的所有联系人")
    @RequestMapping(value = "list/{id}", method = RequestMethod.POST)
    public ResultUtil listAll(@PathVariable("id") Integer id) {

        QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cont_customer_id", id);
        List<Contact> list = contactService.list(queryWrapper);

        List<Customer> list1 = customerService.list();

        for (Contact contact : list) {
            for (Customer customer : list1) {
                if (contact.getContCustomerId().equals(customer.getCustomerId())) {
                    contact.setCustomer(customer);
                }
            }
        }

        return ResultUtil.resObj(list);

    }

    /**
     * 更新回显
     */
    @ApiImplicitParam(name = "id", value = "联系人的id")
    @ApiOperation(value = "更新回显")
    @RequestMapping(value = "toUpdate/{id}", method = RequestMethod.POST)
    public ResultUtil toUpdate(@PathVariable("id") Integer id) {
        //获取联系人
        Contact byId = contactService.getById(id);
        //获取客户信息
        Customer byId1 = customerService.getById(byId.getContCustomerId());

        //外键
        byId.setCustomer(byId1);

        return ResultUtil.resObj(byId);

    }

    @ApiOperation(value = "更新客户联系人")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResultUtil update(Contact contact) {

        boolean b = contactService.updateById(contact);

        if (b) {
            return ResultUtil.resMsgOK("更新成功");
        } else {
            return ResultUtil.resMsgError("更新失败");
        }
    }

    @ApiImplicitParam(name = "id", value = "客户的id")
    @ApiOperation(value = "删除客户联系人")
    @RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
    public ResultUtil del(@PathVariable("id") Integer id) {

        if (contactService.removeById(id)) {
            return ResultUtil.resMsgOK("删除成功");
        } else {
            return ResultUtil.resMsgError("删除失败");
        }
    }

    @ApiOperation(value = "插入一个联系人")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResultUtil add(Contact contact) {
        if (contactService.save(contact)) {
            return ResultUtil.resMsgOK("插入成功");
        } else {
            return ResultUtil.resMsgError("插入失败");
        }
    }
}
